#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n = nums.size(), addmax = -3e4, addmin = 3e4, sum = 0;
        vector<int> fp(n + 1), gp(n + 1);
        for (int i = 0; i < n; i++)
        {
            fp[i + 1] = max(fp[i] + nums[i], nums[i]);
            gp[i + 1] = min(gp[i] + nums[i], nums[i]);
            sum += nums[i];
        }
        int fmax = fp[1], gmin = gp[1];
        for (int i = 2; i <= n; i++)
        {
            fmax = max(fp[i], fmax);
            gmin = min(gmin, gp[i]);
        }
        int ret = (sum - gmin == 0 ? -3e4 : sum - gmin);
        return max(fmax, ret);
    }
};